package com.kyrie.lib_sys.utils.learn.TimeDemo;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TimeDemo02 {
    public static List<Map<String, String>> timeList = new ArrayList<>();
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public static void main(String[] args) {
        timeList.add(Map.of(
                "prepareTime", "2025-07-01 09:00:00",
                "travelTime", "2025-07-01 09:00:01",
                "startTime", "2025-07-01 09:00:02",
                "completeTime", "2025-07-01 09:00:03"
        ));

        timeList.add(Map.of(
                "prepareTime", "2025-07-01 09:00:10",
                "travelTime", "2025-07-01 09:00:11",
                "startTime", "2025-07-01 09:00:12",
                "completeTime", "2025-07-01 09:00:13"
        ));


        //将时间值改成从timeList中获取来判断,而不是自己传递一个时间值
        System.out.println(timeCheck("2025-07-01 09:00:14")); // false
        System.out.println(timeCheck("2025-07-01 09:00:03")); // true (在第一个时间段内)
        System.out.println(timeCheck("2025-07-01 09:00:11")); // true (在第二个时间段内)
        System.out.println(timeCheck("2025-07-01 09:00:09")); // false (在两个时间段之间)

    }


    public static boolean timeCheck(String time) {
        LocalDateTime checkTime = LocalDateTime.parse(time, formatter);

        for (Map<String, String> map : timeList) {
            //起始时间不一定是prepareTime 终止时间不一定是completeTime  它可能只有4个时间值里面的其中一个或者多个;
            //要获取特定的事件段;
            LocalDateTime start = LocalDateTime.parse(map.get("prepareTime"), formatter);
            LocalDateTime end = LocalDateTime.parse(map.get("completeTime"), formatter);

            // 检查时间是否在这个时间段内
            if ((checkTime.isEqual(start) || checkTime.isAfter(start)) &&
                    (checkTime.isEqual(end) || checkTime.isBefore(end))) {
                System.out.println("时间 " + time + " 与时间段 " + map.get("prepareTime") + " 到 " +
                        map.get("completeTime") + " 重叠");
                return true;
            }
        }
        return false;
    }


}

